﻿// -----------------------------------------------------------------------
// <copyright company="www.3WayWebdesign.de">
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
// 
// <author>Michael Morbach</author>
// <email>Michael@3waywebdesign.de</email>
// <web>http://www.3waywebdesign.de</web>
// ----------------------------------------------------------------------

using System.Windows;
using System.Windows.Media;
using Tww.Wpf.Controls.Controls.Shared;

namespace Tww.Wpf.Controls.Controls.Common
{
    public class ToggleButton : System.Windows.Controls.Primitives.ToggleButton
    {
        #region  - Felder (öffentlich) -

        /// <summary>
        ///     The bubble margin property
        /// </summary>
        public static readonly DependencyProperty BubblePaddingProperty =
            DependencyProperty.Register("BubblePadding", typeof (Thickness), typeof (ToggleButton),
                new FrameworkPropertyMetadata(null));

        /// <summary>
        ///     The bubble text property
        /// </summary>
        public static readonly DependencyProperty BubbleTextProperty =
            DependencyProperty.Register("BubbleText", typeof (string), typeof (ToggleButton),
                new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                    BubbleTextPropertyChangedCallback));

        /// <summary>
        ///     The hover background brush property
        /// </summary>
        public static readonly DependencyProperty HoverBackgroundBrushProperty =
            DependencyProperty.Register("HoverBackgroundBrush", typeof (Brush), typeof (ToggleButton),
                new FrameworkPropertyMetadata(null));

        /// <summary>
        ///     The pressed background brush property
        /// </summary>
        public static readonly DependencyProperty CheckedBackgroundBrushProperty =
            DependencyProperty.Register("CheckedBackgroundBrush", typeof(Brush), typeof(ToggleButton),
                new FrameworkPropertyMetadata(null));

        /// <summary>
        ///     The show bubble text property
        /// </summary>
        public static readonly DependencyProperty ShowBubbleTextProperty =
            DependencyProperty.Register("ShowBubbleText", typeof (bool), typeof (ToggleButton),
                new FrameworkPropertyMetadata(true));

        public static readonly DependencyProperty EnableForegroundHiglightingProperty=
            DependencyProperty.Register("EnableForegroundHiglighting", typeof(bool), typeof(ToggleButton),
                new FrameworkPropertyMetadata(true));

        #endregion

        #region  - Konstruktoren -

        static ToggleButton()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof (ToggleButton),
                new FrameworkPropertyMetadata(typeof (ToggleButton)));
        }

        #endregion

        #region  - Konstruktoren -

        public ToggleButton()
        {
            Loaded += OnLoaded;
        }

        #endregion

        #region  - Indexer und Eigenschaften (öffentlich) -

        /// <summary>
        ///     Gets or sets a value indicating whether [show bubble text].
        /// </summary>
        /// <value>
        ///     <c>true</c> if [show bubble text]; otherwise, <c>false</c>.
        /// </value>
        public bool ShowBubbleText
        {
            get { return (bool) GetValue(ShowBubbleTextProperty); }
            set { SetValue(ShowBubbleTextProperty, value); }
        }


        /// <summary>
        /// Gets or sets a value indicating whether [enable foreground higlighting].
        /// </summary>
        /// <value>
        /// <c>true</c> if [enable foreground higlighting]; otherwise, <c>false</c>.
        /// </value>
        public bool EnableForegroundHiglighting
        {
            get { return (bool)GetValue(EnableForegroundHiglightingProperty); }
            set { SetValue(EnableForegroundHiglightingProperty, value); }
        }


        /// <summary>
        ///     Gets or sets the bubble text.
        /// </summary>
        /// <value>
        ///     The bubble text.
        /// </value>
        public string BubbleText
        {
            get { return (string) GetValue(BubbleTextProperty); }
            set { SetValue(BubbleTextProperty, value); }
        }

        /// <summary>
        ///     Gets or sets the bubble margin.
        /// </summary>
        /// <value>
        ///     The bubble margin.
        /// </value>
        public Thickness BubblePadding
        {
            get { return (Thickness) GetValue(BubblePaddingProperty); }
            set { SetValue(BubblePaddingProperty, value); }
        }


        /// <summary>
        ///     Gets or sets the hover background brush.
        /// </summary>
        /// <value>
        ///     The hover background brush.
        /// </value>
        public Brush HoverBackgroundBrush
        {
            get { return (Brush) GetValue(HoverBackgroundBrushProperty); }
            set { SetValue(HoverBackgroundBrushProperty, value); }
        }


        /// <summary>
        /// Gets or sets the checked background brush.
        /// </summary>
        /// <value>
        /// The checked background brush.
        /// </value>
        public Brush CheckedBackgroundBrush
        {
            get { return (Brush)GetValue(CheckedBackgroundBrushProperty); }
            set { SetValue(CheckedBackgroundBrushProperty, value); }
        }

        #endregion

        #region  - Methoden (privat) -

        private static void BubbleTextPropertyChangedCallback(DependencyObject dependencyObject,
            DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            var toggleButton = (ToggleButton) dependencyObject;
            var newBubbleText = (dependencyPropertyChangedEventArgs.NewValue == null)
                ? string.Empty
                : (string) dependencyPropertyChangedEventArgs.NewValue;

            toggleButton.RefreshBubbleState(newBubbleText);
        }

        private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
        {
            RefreshBubbleState(BubbleText);
        }

        /// <summary>
        ///     Refreshes the state of the button bubble and informs the VisualStateManager.
        /// </summary>
        /// <param name="text">The Bubble text.</param>
        private void RefreshBubbleState(string text)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                VisualStateManager.GoToState(this, VisualStates.StateBubbleHide, true);
            }
            else
            {
                VisualStateManager.GoToState(this, VisualStates.StateBubbleShow, true);
            }
        }

        #endregion
    }
}